package com.life.ai.server.modules.develop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.life.ai.server.common.domain.PageQuery;
import com.life.ai.server.framework.cron.CronJob;
import com.life.ai.server.framework.cron.CronJobInterface;
import com.life.ai.server.modules.develop.entity.SysJob;
import com.life.ai.server.modules.develop.enums.SysJobStatusEnum;
import com.life.ai.server.modules.develop.mapper.SysJobMapper;
import com.life.ai.server.modules.develop.service.SysJobService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 基础表结构 服务实现类
 * </p>
 *
 * @author kyle
 * @since 2025-01-06
 */
@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements SysJobService, CronJobInterface {

    @Override
    public List<CronJob> loadCronJobs() {
        List<SysJob> sysJobs = lambdaQuery().eq(SysJob::getStatus, SysJobStatusEnum.RUNNING.getCode())
                .isNotNull(SysJob::getInvokeTarget)
                .isNotNull(SysJob::getCronExpression)
                .orderByAsc(SysJob::getSort).list();
        return CollStreamUtil.toList(sysJobs, sysJob -> BeanUtil.copyProperties(sysJob, CronJob.class));
    }

    /**
     * 分页查询
     *
     * @param pageQuery 分页参数
     * @param sysJob    查询条件
     * @return Page<SysJob>
     */
    @Override
    public Page<SysJob> queryPage(PageQuery pageQuery, SysJob sysJob) {
        return lambdaQuery().page(pageQuery.build());
    }
}

